package org.jypj.dev.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.jypj.dev.dao.IRoleMenuDao;
import org.jypj.dev.dao.IRoleTreeDao;
import org.jypj.dev.model.Menu;
import org.jypj.dev.service.IMenuService;
import org.jypj.dev.service.IRoleMenuService;
import org.jypj.dev.util.StringUtil;
import org.jypj.dev.vo.RoleMenuVo;
import org.jypj.dev.vo.RoleTree;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("roleMenuController")
@Controller
public class RoleMenuController {

	
	@Resource
	private IMenuService menuService;

	@Resource
	private IRoleTreeDao roleTreeDao;
	
	@Resource
	private IRoleMenuService roleMenuService;
	
	@Resource
	private IRoleMenuDao roleMenuDao;
	
	@RequestMapping("toRoleMenu.action")
	public String toRoleMenu() {

		return "back/sys/role_menu_main";
	}

	@RequestMapping("toRoleMenuList.action")
	public String toRoleMenuList(HttpServletRequest request,String roleType) {
		Menu menu =new Menu();
		menu.setType("1");
		menu.setRoleType(roleType);
		List<Menu> menus = menuService.selectAllByMenu(menu);
		String menuStr = roleMenuDao.selectMenusByRoleType(roleType);
		if(StringUtil.isNotEmpty(menuStr)){
			for(Menu m:menus){
				if(menuStr.contains(m.getId())){
					m.setIsCheck("true");
				}
			}
		}
		request.setAttribute("menus", menus);
		request.setAttribute("roleType", roleType);
		return "back/sys/role_menu_list";
	}
	/**
	 * 查询角色菜单
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectAllRole.action")
	@ResponseBody
	public Map<String, Object> selectAllRole(String roleType) {
		Map<String, Object> map = new HashMap<>();
		int code = 0;

		try {
			List<RoleTree> roleTrees = roleTreeDao.selectAllRoleTree();
			map.put("datas", roleTrees);

		} catch (Exception e) {
			e.printStackTrace();
			code=-1;
		}

		map.put("code", code);
		return map;
	}
	/**
	 * 查询角色菜单
	 * 
	 * @return
	 */
	@RequestMapping(value = "saveRoleMenu.action")
	@ResponseBody
	public Map<String, Object> saveRoleMenu(RoleMenuVo roleMenus) {
		Map<String, Object> map = new HashMap<>();
		int code = 0;
		try {
			roleMenuService.saveRoleMenus(roleMenus);
		} catch (Exception e) {
			e.printStackTrace();
			code=-1;
		}
		map.put("code", code);
		return map;
	}
	
}
